<?php

namespace app\common\exceptions;

use app\common\exceptions\RetCode;
use app\common\facade\Log;
use Exception;

class ErrException extends Exception
{
    /***
     * 抛异常函数
     * @param $code
     * @param $message
     * @return mixed
     * @throws ErrException
     */
    public static function throwExcep($code, string $message = '')
    {
        static $errorCode = 9990;
        if (is_numeric($code) && isset(RetCode::$err_msg[$code])) {
            $message = !empty($message) ? $message : RetCode::$err_msg[$code];
        } else if (is_string($code)) {
            $message = '[ERROR_SYSTEM]错误' . $code;
            $code = ++$errorCode;
        }else if($code instanceof Exception){
            throw $code;
        }

        // 记录异常日志
        Log::error($code);
        if (is_object($code) && $code instanceof Exception) {
            throw $code;
        } else {
            die(json_encode(['code' => $code, 'msg' => $message],JSON_UNESCAPED_UNICODE));
        }
    }
}

